var jwt = require("jsonwebtoken");
module.exports = options => {
  return async function auth(ctx, next) {
    if (
      ctx.path == "/v1/login" ||
      ctx.path == "/v1/register" ||
      ctx.path == "/v1/app/register" ||
      ctx.path == "/redirect"
    ) {
      await next();
      return;
    }

    if (ctx.path.indexOf("/v1/test") > -1) {
      await next();
      return;
    }
    if (ctx.path.indexOf("/v1/agent") > -1) {
      await next();
      return;
    }
    if (ctx.path.indexOf("/v1/exam") > -1) {
      await next();
      return;
    }

    try {
      const token = ctx.cookies.get("token", {
        signed: false,
        encrypt: true
      });
      var decoded = jwt.verify(token, "aaa");
      await next();
    } catch (err) {
      console.log(err)
      ctx.body = {
        code: -90001,
        msg: "未登录"
      };
    }

    // if (!ctx.session || !ctx.session.userId){
    //   // ctx.redirect('/login');
    //
    // }
  };
};
